/*!
 Temelia - Splay tree samples.

 Copyright (C) 2008 Ceata (http://ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/samples.h"
#include <temelia/splay_tree.h>

static void sample1()
{
	int **x, i, N = 100;
	splay_tree_t splay;

	PRINT("\n\n Sample 1 started \n\n");

	x = int_array_new(N, N);
	splay = splay_tree_new(x[0]);

	for (i = 0; i < N; i++)
		splay_tree_delete_node(splay_tree_new(NULL));

	for (i = 0; i < N; i++)
		splay = splay_tree_insert(splay, x[i], int_compare, NULL);

	PRINT("Compare %d\n", splay_tree_compare_trees(splay, splay, int_compare, NULL));

	PRINT("\nPreorder :\n");
	splay_tree_preorder(splay, int_handler, NULL);

	PRINT("\nInorder :\n");
	splay_tree_inorder(splay, int_handler, NULL);

	PRINT("\nReverse inorder :\n");
	splay_tree_reverse_inorder(splay, int_handler, NULL);

	PRINT("\nPostorder :\n");
	splay_tree_postorder(splay, int_handler, NULL);

	PRINT("\nLevel order :\n");
	splay_tree_level_order(splay, int_handler, NULL);

	PRINT("\nShow indented :\n");
	splay_tree_show_indented(splay, int_level_handler, NULL);

	PRINT("Empty %d, Leaf %d\n", splay_tree_is_empty(splay),
			splay_tree_is_leaf(splay));

	for (i = 0; i < N; i++)
	{
		if (splay_tree_search(splay, x[i], int_compare, NULL) == NULL)
		{
			PRINT("Splay error, check this line %d\n", __LINE__);
			//exit(1);
		}
	}

	for (i = 0; i < N; i++)
		splay = splay_tree_remove(splay, x[i], int_compare, NULL);

	PRINT("%d %p %p %p %p %p %d %d %p %p\n", splay_tree_get_size(splay),
			splay_tree_get_root(splay), splay_tree_get_key(splay),
			splay_tree_get_parent(splay), splay_tree_get_left_child(splay),
			splay_tree_get_right_child(splay), splay_tree_get_height(splay),
			splay_tree_get_depth(splay), splay_tree_get_min(splay),
			splay_tree_get_max(splay));

	splay_tree_delete(splay);

	int_array_delete(x, N);

	PRINT("\n\n Sample 1 ended \n\n");
}

void run_splay_tree_samples()
{
	sample1();
}
